package org.example;
//一个子类 实现一个父接口
//子类一定会强制重写父接口里面的方法
//当调用这个子类里面的方法的时候 就会执行这个类里面的代码
//分层：3层
//        1.controller    接受页面的请求
//        2.service       处理逻辑
//        3.dap           操作数据库

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Vector;

public class UserDaoImpl {
    public Vector<Vector<Object>> getStudentAll(){
        Connection conn = JdbcUtils.getConnection();
        ResultSet result = null;
        PreparedStatement pst= null;
        String sql = "SELECT * FROM student WHERE `status` = 1";
        Vector<Vector<Object>> resultVector = new Vector<Vector<Object>>();
        try {

             pst = conn.prepareStatement(sql);
            result = pst.executeQuery();
            while ((result.next())){
                Vector<Object> vector = new Vector<Object>();
                vector.add(result.getInt("id"));
                vector.add(result.getString("name"));
                vector.add(result.getDouble("score"));
                vector.add(result.getString("nationality"));
                boolean gender = result.getBoolean("gender");
                if(gender){
                    vector.add("男");
                }
                else 
                    vector.add("女");

                Date date = result.getDate("admission_time");
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String admission_time = format.format(date);
                vector.add(admission_time);

                resultVector.add(vector);
            }
            return resultVector;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.closeAll(conn,pst,result);
        }
    }

    public boolean login(String name, String pass) {
        Connection conn = JdbcUtils.getConnection();
        PreparedStatement pst = null;
        ResultSet result = null;
        String sql = "SELECT * FROM User WHERE user_name = ? AND pass_word = ? AND  status = 1";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,name);
            pst.setString(2,pass);
            result = pst.executeQuery();
            if(result.next()){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //表示一定会执行的代码
            JdbcUtils.closeAll(conn,pst,result);
        }
        return false;
    }

    public  boolean Delete(int ID,String Name) throws ClassNotFoundException {
        PreparedStatement pst=null;
        ResultSet result=null;
        Connection conn = JdbcUtils.getConnection();
        String sql ="DELETE FROM student WHERE id = ? AND name = ? ";
        //SELECT * FROM user WHERE user_name = "zjl" AND pass_word ="zjl" AND is_delete = 1
        //这里实际是把页面传来的 username 和 password 替代sql语句中的两个问号（?） 构成完整的sql语句去访问数据库

        try {
            pst =conn.prepareStatement(sql);
            pst.setInt(1,ID);//这里的name和下面的pass 都是从页面那里传来的
            pst.setString(2,Name);

            int i=pst.executeUpdate();
            System.out.println(i);
            if (i==1){
                System.out.println("删除成功");
                return  true;
            }
            else{
                System.out.println("删除失败");
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  false;
    }
    //删除时返回删除学生的名字
    public  String Student_name(int ID,String Name) throws ClassNotFoundException {
        String nick_name=null;
        PreparedStatement pst=null;
        ResultSet result=null;
        Connection conn = JdbcUtils.getConnection();
        String sql ="SELECT * FROM student WHERE id = ? AND name = ?";
        try {
            pst =conn.prepareStatement(sql);
            pst.setInt(1,ID);//这里的name和下面的pass 都是从页面那里传来的
            pst.setString(2,Name);
            result=pst.executeQuery();
            if (result.next()){
                nick_name = result.getString("name");
                return nick_name;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  "有误";
    }
}
